package gamemodel;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Observable;

public class Ball extends Observable implements Collidable  {

	private Coordinate position;  
	private transient Vector2d velocity;  
	private transient int exp; 
	
	private static final int DEFAULT_EXPONENT = 0 ; 
	private static final double DEFAULT_BASE = 0.9; 
	private static final int MAX_VELOCITY = 9; 
	public static final float RADIUS = 13; 
	private static final float MASS = 1; 
	
	public Ball(float x, float y){
		this.position = new Coordinate(x,y); 
		this.velocity = new Vector2d(0, 0); 
		this.exp = DEFAULT_EXPONENT; 
	}
	
	public void setPosition(float x, float y){
		position.set(x, y); 
		this.setChanged(); 
		this.notifyObservers(position); 
	}
	
	public void setVelocity(float x, float y){
		this.velocity.set(x, y); 
	}

	@Override
	public float getMass() {
		return Ball.MASS; 
	}

	@Override
	public float getRadius() {
		return Ball.RADIUS;
	}

	@Override
	public Coordinate getPosition() {
		return this.position; 
	}

	@Override
	public Vector2d getVelocity() {
		return this.velocity;
	}

	@Override
	public void setPosition(Vector2d v) {
		this.position.set(v.getX(), v.getY());
		this.setChanged(); 
		this.notifyObservers(position); 
		
	}

	@Override
	public void setVelocity(Vector2d v) {
		this.velocity = v; 
		
	}
	
	public void resetExponent(){
		this.exp = DEFAULT_EXPONENT; 
	}
	
	public void performMoveHelper(){
		
		float vx = this.velocity.getX() * (float) Math.pow(DEFAULT_BASE, exp - 15) ;
		float vy = this.velocity.getY() * (float) Math.pow(DEFAULT_BASE, exp - 15) ;
		this.exp++;
		
		
		//set a max limit on what the velocity could be
		if (vx > MAX_VELOCITY){
			vx = MAX_VELOCITY; 
		} else if (vx < -1 * MAX_VELOCITY){
			vx = -1 * MAX_VELOCITY; 
		}
		if (vy > MAX_VELOCITY){
			vy = MAX_VELOCITY; 
		} else if (vy < -1 * MAX_VELOCITY){
			vy = -1 * MAX_VELOCITY; 
		}
		
		position.translate(vx, vy); 
		this.velocity.set(vx, vy); 
		if (TestConstant.DEBUG)	System.out.println("ball: " + " x:" + this.getPosition().getX() + " y:" + this.getPosition().getY() + " vx:"+ this.velocity.getX() + " vy: " + this.velocity.getY()); 

	}
	
	public void paint(Graphics2D g){
		g.setColor(Color.PINK); 
		Ellipse2D.Double circle = new Ellipse2D.Double(this.position.getX() - Ball.RADIUS, this.position.getY() - Ball.RADIUS, Ball.RADIUS * 2, Ball.RADIUS * 2); 
		g.fill(circle); 		
	}
	
	public static void main(String args[]){
//		Player p = new Player("rogan"); 
//		Player p1 = new Player("vijith");
//		
//		Ball b = new Ball(1,2); 
//		b.addObserver(p);
//		b.addObserver(p1);
//		
//		b.setPosition(2, 3);
//		b.setPosition(1, 5);
	}
}
